package com.ofps.forms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ofps.forms.common.utils.R;
import com.ofps.forms.common.utils.TokenUtil;
import com.ofps.forms.mapper.QuestionCheckboxMapper;
import com.ofps.forms.domain.QuestionCheckbox;
import com.ofps.forms.service.QuestionCheckboxService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.ofps.common.utils.SecurityUtils.getUserId;


@Service("questionCheckboxService")
public class QuestionCheckboxServiceImpl extends ServiceImpl<QuestionCheckboxMapper, QuestionCheckbox> implements QuestionCheckboxService {


    @Resource
    private QuestionCheckboxMapper questionCheckboxMapper;

    @Override
    public R save(HttpServletRequest request, QuestionCheckbox questionCheckbox) {
//        questionCheckbox.setUserId(TokenUtil.getId(request));
        questionCheckbox.setUserId(Math.toIntExact(getUserId()));
        return this.save(questionCheckbox) ? R.ok("保存成功") : R.error("保存失败");
    }

    public R delete(Map<String, Object> body) {
        QueryWrapper<QuestionCheckbox> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", body.get("id"));
        return questionCheckboxMapper.delete(queryWrapper) == 1 ? R.ok("删除成功！") : R.error("删除失败！");
    }

    @Override
    public List<QuestionCheckbox> queryWithId(Map<String, Object> body) {
        QueryWrapper<QuestionCheckbox> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", body.get(body.get("id")));
        List<QuestionCheckbox> questionCheckboxList = questionCheckboxMapper.selectList(queryWrapper);
        return questionCheckboxList;
    }

}
